package com.example.demo1122.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo1122.DTO.CourseDTO;
import com.example.demo1122.domain.ResponseResult;
import com.example.demo1122.entity.Chapter;
import com.example.demo1122.entity.Course;
import com.example.demo1122.service.IChapterService;
import com.example.demo1122.service.IConversationService;
import com.example.demo1122.service.ICourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-12-09
 */
@Controller
@RequestMapping("/course")
public class CourseController {
    @Resource
    private ICourseService courseService;
    @Resource
    private IChapterService chapterService;
    @Resource
    private IConversationService conversationService;

    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);
    // 新增和修改
    @PostMapping("/saveorupdate")
    @ResponseBody
    public ResponseResult save(@RequestParam(defaultValue = "0") Integer courseID,
                               @RequestParam String courseName,
                               @RequestParam(defaultValue = "") String sysRole) {
        try {
           // 新增或者更新
            Course course=new Course();
            course.setCourseName(courseName);
            course.setCourseID(courseID);
            course.setSysRole(sysRole);
            System.out.println("测试语句："+courseID);
            return courseService.saveCourse(course);
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();
            // 处理异常，将异常信息放入ResponseResult对象返回
            return new ResponseResult<>(500, "插入失败", null);
        }
    }

    private List<CourseDTO> convertToDTOList(List<Course> courses) {

        List<CourseDTO> courseDTOs = new ArrayList<>();

        for (Course course : courses) {
            CourseDTO courseDTO = new CourseDTO();
            courseDTO.setCourseID(course.getCourseID());
            courseDTO.setCourseName(course.getCourseName());

            // 如果有其他需要转换的字段，可以继续添加

            courseDTOs.add(courseDTO);
        }

        return courseDTOs;
    }

    @GetMapping("/findall")
    @ResponseBody
    public List<CourseDTO> findAll() {
        List<Course> courses = courseService.list();
        List<CourseDTO> courseDTOs = convertToDTOList(courses);
        return courseDTOs;
    }

    //删除单个数据
    @DeleteMapping("/{courseID}")
    @ResponseBody
    public ResponseResult delete(@PathVariable Integer courseID) {

        try {
            courseService.removeById(courseID);
            return new ResponseResult(156, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(157, "删除失败");
        }
    }

    //批量删除
    @PostMapping("/del/batch")
    @ResponseBody
    public ResponseResult deleteBatch(@RequestBody List<Integer> courseIDs) { // [1,2,3]
        try {
        courseService.removeByIds(courseIDs);
            return new ResponseResult(106, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(107, "删除失败");
        }
    }
    // 分页查询
    @GetMapping("/page2")
    @ResponseBody
    public ResponseResult findPage2(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") Integer courseID,
                                    @RequestParam(defaultValue = "") String name) {
        try {
            Page<Course> page = courseService.findPage(new Page<>(pageNum, pageSize), courseID,name);
            List<CourseDTO> dtos = convertToDTOList(page.getRecords());


            return new ResponseResult<>(166, "查询成功", dtos);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(167, "查询失败");
        }
    }

    // 修改课程SysRole描述
    @PostMapping("/editSysRole")
    @ResponseBody
    public ResponseResult editSysRole(@RequestBody Course course) {
        try {
            return courseService.editSysRole(course);
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();
            // 处理异常，将异常信息放入ResponseResult对象返回
            return new ResponseResult<>(500, "插入失败", e);
        }
    }

    // 编辑课程的章节信息
    @PostMapping("/editChapter")
    @ResponseBody
    public ResponseResult editChapter(@RequestBody Chapter chapter) {
        try{
            return chapterService.insertOrupdateChapter(chapter);
        }
        catch (Exception e){
            return new ResponseResult<>(0, "error", e);
        }
    }

    // 返回某一时间段内该课程的会话数量
    @GetMapping("/getConversationNum")
    @ResponseBody
    public ResponseResult getCourseConversationNum_interval(@RequestParam Integer courseID,
                                                      @RequestParam(required = false) Integer chapterID,
                                                      @RequestParam(required = false) Integer interval){
        return conversationService.getCourseConversationNum_interval(courseID, chapterID, interval);
    }

    // 返回某一时间段内该课程的会话数量
    @GetMapping("/getConversationNumRange")
    @ResponseBody
    public ResponseResult getCourseConversationNum_range(@RequestParam Integer courseID,
                                                        @RequestParam(required = false) Integer chapterID,
                                                        @RequestParam String startSelectTime,
                                                        @RequestParam String endSelectTime){
        return conversationService.getCourseConversationNum_range(courseID, chapterID, startSelectTime, endSelectTime);
    }


}
